﻿using HandmadeShop.BusinessLogic;
using HandmadeShop.BusinessLogic.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HandmadeShop.Application.Controllers
{
    public class BaseController : Controller
    {
        protected readonly string user_id = "user_id";
        protected readonly string user_email = "user_email";

        public BaseController()
        {
            var user = this.GetAuthenticatedUser();
            if (user != null)
            {
                System.Web.HttpContext.Current.Session[Context.SessionName] = user;
            }
        }

        public User GetAuthenticatedUser()
        {
            var cookieId = System.Web.HttpContext.Current.Request.Cookies[user_id];
            if (cookieId == null) return null;

            var cookieEmail = System.Web.HttpContext.Current.Request.Cookies[user_email];
            if (cookieEmail == null) return null;

            Guid id = Guid.Parse(cookieId.Value);
            AccountBL accountBL = new AccountBL();
            var user = accountBL.GetById(id);

            return user;
        }
    }
}
